<template>
	<view class="container">
		<PageTitle title="私教"></PageTitle>
		<view class="flex-x-bet-y-center search-container" style="padding-top: 20rpx;">
			<view class="search-box flex-x-bet-y-center flex1">
				<view class="flex-y-center" @tap="navTo('/pages/teacher/search')">
					<image src="../../static/images/icon-search.png" mode=""></image>
					<text class="input-text">请输入私教名称搜索</text>
				</view>
			</view>

			<view class="flex-column-y-center fitler-icon-wrapper" @tap="showFilter">
				<image src="../../static/images/icon-filter.png" mode=""></image>
				<text>筛选</text>
			</view>
		</view>

		<view class="teacher-list">
			<view class="teacher-item" v-for="(item, index) in tableData" @tap="navTo('/pages/teacher/detail?teacherId=' + item.teacherId)">
				<view class="info flex-x-bet-y-center">
					<view class="flex-y-center">
						<image src="../../static/images/default-avatar.png" v-if="!item.sysUser.avatar" class="avatar" mode=""></image>
						<image :src="item.sysUser.avatar | formatFile" v-else class="avatar" mode=""></image>
						<text class="name">{{item.sysUser.nickName}}</text>
					</view>
					<view class="tag-list flex-y-center">
						<view class="tag-item" v-for="(tag, index) in item.tagName">{{tag}}</view>
					</view>
				</view>
				<view class="intro">{{item.intro}}</view>
				
			</view>
		</view>
		<my-loadmore :currentStep="loadingStep" v-if="loadingStep != 0 && tableData.length"></my-loadmore>
		<my-empty :isShow="loadingStep != 0 && !tableData.length"></my-empty>

		<my-popup ref="filterPopup" :closeable="false" mode="top" :zIndex="2">
			<view class="filter-popup">
				<view class="filter-title">私教分类</view>
				<view class="filter-tags">
					<view class="filter-tag" v-for="(item, index) in teacherTagList" :key="item.tagId" 
							@tap="chooseTag(item)" :class="{'active-tag': isActive(item)}">{{item.tagName}}</view>
				</view>
				<view class="fitler-btns">
					<view class="btn btn-reset" @tap="resetTag">重置</view>
					<view class="btn btn-ok" @tap="queryFilter">确认</view>
				</view>
			</view>
		</my-popup>
	</view>
</template>

<script>
	import { getTeacherList, getTeacherTagList } from '@/api/common.js'
	export default {
		data() {
			return {
				choosedTagList: [],
				teacherTagList:[],
			};
		},
		onLoad(option) {
			this.getTeacherList()
			this.getTeacherTagList()
		},
		methods: {
			showFilter() {
				this.$refs.filterPopup.open()
			},
			resetTag(){
				this.$refs.filterPopup.close()
				this.choosedTagList = []
				this.initQuery();
				this.getTeacherList()
			},
			queryFilter(){
				const tagIds = this.choosedTagList.map(item => item.tagId).join(",")
				this.initQuery();
				if(tagIds.length){
					this.queryParams.tagIdList = tagIds;
				}
				this.$refs.filterPopup.close()
				this.getTeacherList()
			},
			chooseTag(item){
				let index = -1;
				for (var i = 0; i < this.choosedTagList.length; i++) {
					if(this.choosedTagList[i].tagId === item.tagId){
						index = i
						break;
					}
				}
				
				if(index == -1){
					this.choosedTagList.push(item)
					console.log(this.choosedTagList);
				}else{
					this.choosedTagList.splice(index, 1)
				}
			},
			getTeacherList(){
				this.queryParams.status = 0
				this.queryList(getTeacherList, data => {
					data.forEach(item => {
						item.tagName = item.tagName.split(',')
					})
				})
			},
			getTeacherTagList(){
				getTeacherTagList({pageNum: 1, pageSize: 999}).then(res => {
					this.teacherTagList = res.rows
				})
			},
			isActive(item){
				let index = -1;
				for (var i = 0; i < this.choosedTagList.length; i++) {
					if(this.choosedTagList[i].tagId === item.tagId){
						index = i
					}
				}
				return index >= 0;
			}
		}
	}
</script>

<style lang="scss">
	.container {
		width: auto;
		background-color: #f8f8f8;
		padding-bottom: 0;
	}

	.search-container {
		position: relative;
		z-index: 3;
		background-color: #fff;
		padding: 50rpx 30rpx 20rpx;
	}

	.search-box {
		border: 2rpx solid $theme-color;
		border-radius: 36rpx;
		height: 73rpx;
		border-radius: 36px;
		margin-right: 30rpx;

		image {
			width: 46rpx;
			height: 46rpx;
			margin: 0 15rpx 0 20rpx;
		}

		.input-text {
			font-size: 28rpx;
			flex: 1;
			color: #666666;
		}

	}

	.fitler-icon-wrapper {
		image {
			width: 42rpx;
			height: 42rpx;
		}

		text {
			font-size: 24rpx;
			font-weight: bold;
			color: #333333;
		}
	}


	.filter-popup {
		padding: 70rpx 0rpx 0rpx;
		background-color: #fff;

		.filter-title {
			font-size: 30rpx;
			font-weight: bold;
			color: #333333;
			padding-left: 30rpx;
		}

		.filter-tags {
			display: flex;
			flex-wrap: wrap;
			padding: 0 30rpx 30rpx 0;

			.filter-tag {
				display: flex;
				justify-content: center;
				align-items: center;
				padding: 10rpx 30rpx;
				background: #F8F9FB;
				border-radius: 35rpx;
				font-size: 30rpx;
				font-weight: 500;
				color: #333333;
				margin: 30rpx;
				margin-bottom: 0;
			}

			.filter-tag:nth-of-type(3n) {
				margin-right: 0;
			}
			
			.active-tag{
				background-color: #FFFAF7;
				color: $theme-color;
			}
		}
		
		.fitler-btns {
			display: flex;
		
			.btn {
				line-height: 100rpx;
				text-align: center;
				flex: 1;
				font-size: 38rpx;
				font-weight: 500;
				color: #FFFFFF;
			}
		
			.btn-reset {
				background: #343247;
			}
		
			.btn-ok {
				background-color: $theme-color;
			}
		}
	}

	/deep/ .u-transition {
		top: 135px !important;
	}

	.teacher-list {
		padding: 30rpx;
		padding-top: 0;

		.teacher-item {
			padding: 40rpx 30rpx;
			margin-top: 30rpx;
			background: #FFFFFF;
			box-shadow: 0px 6rpx 35rpx 0px rgba(0, 0, 0, 0.1);
			border-radius: 26rpx;

			.info {
				display: flex;

				.avatar {
					width: 92rpx;
					height: 92rpx;
					margin-right: 30rpx;
					border-radius: 50%;
					flex-shrink: 0;
					
				}

				.name {
					font-size: 32rpx;
					font-weight: bold;
					color: #333333;
					flex-shrink: 0;
				}

				.tag-list {
					display: flex;
					flex-wrap: wrap;
					justify-content: flex-end;
					margin-left: 30rpx;
					
					.tag-item {
						padding: 4rpx 10rpx;
						background-color: $theme-color;
						font-size: 24rpx;
						color: #fff;
						border-radius: 6rpx;
						margin-right: 15rpx;
						margin-top: 10rpx;
					}
				}
			}

			.intro {
				padding-top: 20rpx;
				font-size: 28rpx;
				font-weight: 500;
				color: #666666;
				line-height: 44rpx;
				overflow : hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 5;
				-webkit-box-orient: vertical;
			}
		}
	}
</style>